/*
 *  Javascript Framework - Debug
 *
 * Copyright (c) 2007 Sina.com
 *
 * $Id: Debug.js 971 2007-11-09 10:51:18Z libing $
 */
 
Array.prototype.debug = function (names, indent) {
    var a, b, gap = (indent || '') + '    ', i, l = this.length, v;

    function p(s) {
        if (b) {
            a.push(',');
        }
        a.push('\n', gap, s);
        b = true;
    }

    function q(v, x) {
        switch (typeof v) {
        case 'undefined':
        case 'function':
        case 'unknown':
            return '';
        case 'object':
            if (v) {
                if (typeof v.debug === 'function') {
                    return v.debug(names, x);
                }
            } else {
                return 'null';
            }
            break;
        default:
            return v.debug(names, x);
        }
    }

    if (l === 1) {
        return '[' + q(this[0], indent) + ']';
    }

    a = ['['];
    for (i = 0; i < l; i += 1) {
        v = q(this[i], gap);
        if (typeof v === 'string') {
            p(v);
        }
    }
    if (b) {
        a.push('\n', indent);
    }
    a.push(']');
    return a.join('');
};

Boolean.prototype.debug = Number.prototype.debug = function () {
    return String(this);
};

Date.prototype.debug = function () {

    function f(n) {
        return n < 10 ? '0' + n : n;
    }

    return '"' + this.getFullYear() + '-' +
            f(this.getMonth() + 1) + '-' +
            f(this.getDate()) + 'T' +
            f(this.getHours()) + ':' +
            f(this.getMinutes()) + ':' +
            f(this.getSeconds()) + '"';
};

Object.prototype.debug = function (names, indent) {
    var a = ['{'], b, gap = (indent || '') + '    ', i, n, v;

    function p(s) {
        if (b) {
            a.push(',');
        }
        a.push('\n', gap, n, ': ', s);
        b = true;
    }

    if (!names) {
        names = [];
        for (i in this) {
            if (this.hasOwnProperty(i)) {
                names.push(i);
            }
        }
        names = names.sort();
    }

    for (i = 0; i < names.length; i += 1) {
        n = names[i];
        v = this[n];
        switch (typeof v) {
        case 'undefined':
        case 'function':
        case 'unknown':
            break;
        case 'object':
            if (v) {
                if (typeof v.debug === 'function') {
                    p(v.debug(names, gap));
                }
            } else {
                p("null");
            }
            break;
        default:
            p(v.debug(names, gap));
        }
    }
    if (b) {
        a.push('\n', indent);
    }
    a.push('}');
    return a.join('');
};

(function () {
    var m = {
        '\b': '\\b',
        '\t': '\\t',
        '\n': '\\n',
        '\f': '\\f',
        '\r': '\\r',
        '"' : '\\"',
        '\\': '\\\\'
    };

    String.prototype.debug = function () {
        if (/["\\\x00-\x1f]/.test(this)) {
            return '"' + this.replace(/([\x00-\x1f\\"])/g, function (a, b) {
                var c = m[b];
                if (c) {
                    return c;
                }
                c = b.charCodeAt();
                return '\\u00' +
                    Math.floor(c / 16).toString(16) +
                    (c % 16).toString(16);
            }) + '"';
        }
        return '"' + this + '"';
    };
})();

function vTrace(obj)
{
    if ( arguments.length == 2 )
        return obj.debug();
    else
    {
        var div = document.createElement( 'div' ); 
        document.body.appendChild( div, null ); 
    	
    	div.innerHTML = obj.debug();
    	div.style.border = '2px solid #FF0000';
    }
}

